package com.mobclick.android;

import android.content.ContentResolver;
import android.content.Context;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.RequestWrapper;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;

public final class r
  implements HttpClient
{
  public static long a = 0L;
  private static final int b = 60000;
  private static final String c = "AndroidHttpClient";
  private static final HttpRequestInterceptor d = new p();
  private final HttpClient e;
  private RuntimeException f = new IllegalStateException("AndroidHttpClient created and never closed");
  private volatile t g;

  private r(ClientConnectionManager paramClientConnectionManager, HttpParams paramHttpParams)
  {
    this.e = new q(this, paramClientConnectionManager, paramHttpParams);
  }

  public static long a(ContentResolver paramContentResolver)
  {
    return a;
  }

  public static r a(String paramString)
  {
    return a(paramString, null);
  }

  public static r a(String paramString, Context paramContext)
  {
    BasicHttpParams localBasicHttpParams = new BasicHttpParams();
    HttpConnectionParams.setStaleCheckingEnabled(localBasicHttpParams, false);
    HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 60000);
    HttpConnectionParams.setSoTimeout(localBasicHttpParams, 60000);
    HttpConnectionParams.setSocketBufferSize(localBasicHttpParams, 8192);
    HttpClientParams.setRedirecting(localBasicHttpParams, false);
    HttpProtocolParams.setUserAgent(localBasicHttpParams, paramString);
    SchemeRegistry localSchemeRegistry = new SchemeRegistry();
    localSchemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    return new r(new ThreadSafeClientConnManager(localBasicHttpParams, localSchemeRegistry), localBasicHttpParams);
  }

  public static InputStream a(HttpEntity paramHttpEntity)
  {
    Object localObject = paramHttpEntity.getContent();
    if (localObject == null);
    while (true)
    {
      String str;
      do
      {
        Header localHeader;
        do
        {
          return localObject;
          localHeader = paramHttpEntity.getContentEncoding();
        }
        while (localHeader == null);
        str = localHeader.getValue();
      }
      while ((str == null) || (!(str.contains("gzip"))));
      localObject = new GZIPInputStream((InputStream)localObject);
    }
  }

  public static AbstractHttpEntity a(byte[] paramArrayOfByte, ContentResolver paramContentResolver)
  {
    Object localObject;
    if (paramArrayOfByte.length < a(paramContentResolver))
      localObject = new ByteArrayEntity(paramArrayOfByte);
    while (true)
    {
      return localObject;
      ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
      GZIPOutputStream localGZIPOutputStream = new GZIPOutputStream(localByteArrayOutputStream);
      localGZIPOutputStream.write(paramArrayOfByte);
      localGZIPOutputStream.close();
      ByteArrayEntity localByteArrayEntity = new ByteArrayEntity(localByteArrayOutputStream.toByteArray());
      localByteArrayEntity.setContentEncoding("gzip");
      localObject = localByteArrayEntity;
    }
  }

  public static void a(HttpRequest paramHttpRequest)
  {
    paramHttpRequest.addHeader("Accept-Encoding", "gzip");
  }

  private static String b(HttpUriRequest paramHttpUriRequest, boolean paramBoolean)
  {
    URI localURI1;
    URI localURI2;
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("curl ");
    Header[] arrayOfHeader = paramHttpUriRequest.getAllHeaders();
    int i = arrayOfHeader.length;
    int j = 0;
    if (j >= i)
    {
      localURI1 = paramHttpUriRequest.getURI();
      if (!(paramHttpUriRequest instanceof RequestWrapper))
        break label289;
      HttpRequest localHttpRequest = ((RequestWrapper)paramHttpUriRequest).getOriginal();
      if (!(localHttpRequest instanceof HttpUriRequest))
        break label289;
      localURI2 = ((HttpUriRequest)localHttpRequest).getURI();
    }
    while (true)
    {
      localStringBuilder.append("\"");
      localStringBuilder.append(localURI2);
      localStringBuilder.append("\"");
      if (paramHttpUriRequest instanceof HttpEntityEnclosingRequest)
      {
        HttpEntity localHttpEntity = ((HttpEntityEnclosingRequest)paramHttpUriRequest).getEntity();
        if ((localHttpEntity != null) && (localHttpEntity.isRepeatable()))
        {
          if (localHttpEntity.getContentLength() >= 1024L)
            break label278;
          ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
          localHttpEntity.writeTo(localByteArrayOutputStream);
          String str = localByteArrayOutputStream.toString();
          localStringBuilder.append(" --data-ascii \"").append(str).append("\"");
        }
      }
      while (true)
      {
        return localStringBuilder.toString();
        Header localHeader = arrayOfHeader[j];
        if ((!(paramBoolean)) && (((localHeader.getName().equals("Authorization")) || (localHeader.getName().equals("Cookie")))));
        while (true)
        {
          while (true)
            ++j;
          localStringBuilder.append("--header \"");
          localStringBuilder.append(localHeader.toString().trim());
          localStringBuilder.append("\" ");
        }
        label278: localStringBuilder.append(" [TOO MUCH DATA TO INCLUDE]");
      }
      label289: localURI2 = localURI1;
    }
  }

  public void a()
  {
    if (this.f != null)
    {
      getConnectionManager().shutdown();
      this.f = null;
    }
  }

  public void a(String paramString, int paramInt)
  {
    if (paramString == null)
      throw new NullPointerException("name");
    if ((paramInt < 2) || (paramInt > 7))
      throw new IllegalArgumentException("Level is out of range [2..7]");
    this.g = new t(paramString, paramInt, null);
  }

  public void b()
  {
    this.g = null;
  }

  public Object execute(HttpHost paramHttpHost, HttpRequest paramHttpRequest, ResponseHandler paramResponseHandler)
  {
    return this.e.execute(paramHttpHost, paramHttpRequest, paramResponseHandler);
  }

  public Object execute(HttpHost paramHttpHost, HttpRequest paramHttpRequest, ResponseHandler paramResponseHandler, HttpContext paramHttpContext)
  {
    return this.e.execute(paramHttpHost, paramHttpRequest, paramResponseHandler, paramHttpContext);
  }

  public Object execute(HttpUriRequest paramHttpUriRequest, ResponseHandler paramResponseHandler)
  {
    return this.e.execute(paramHttpUriRequest, paramResponseHandler);
  }

  public Object execute(HttpUriRequest paramHttpUriRequest, ResponseHandler paramResponseHandler, HttpContext paramHttpContext)
  {
    return this.e.execute(paramHttpUriRequest, paramResponseHandler, paramHttpContext);
  }

  public HttpResponse execute(HttpHost paramHttpHost, HttpRequest paramHttpRequest)
  {
    return this.e.execute(paramHttpHost, paramHttpRequest);
  }

  public HttpResponse execute(HttpHost paramHttpHost, HttpRequest paramHttpRequest, HttpContext paramHttpContext)
  {
    return this.e.execute(paramHttpHost, paramHttpRequest, paramHttpContext);
  }

  public HttpResponse execute(HttpUriRequest paramHttpUriRequest)
  {
    return this.e.execute(paramHttpUriRequest);
  }

  public HttpResponse execute(HttpUriRequest paramHttpUriRequest, HttpContext paramHttpContext)
  {
    return this.e.execute(paramHttpUriRequest, paramHttpContext);
  }

  protected void finalize()
  {
    super.finalize();
    if (this.f != null)
    {
      Log.e("AndroidHttpClient", "Leak found", this.f);
      this.f = null;
    }
  }

  public ClientConnectionManager getConnectionManager()
  {
    return this.e.getConnectionManager();
  }

  public HttpParams getParams()
  {
    return this.e.getParams();
  }
}